package com.longai.controller;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import util.Page;

import com.google.gson.Gson;
import com.alibaba.fastjson.*;
import com.longai.model.Moment;
import com.longai.model.MomentComment;
import com.longai.model.Momentlike;
import com.longai.model.User;
import com.longai.service.MomentService;
import com.longai.service.MomentlikeService;
import com.longai.service.UserService;
import com.longai.vo.MomentCommentVo;
import com.longai.vo.MomentLikeVo;

@Controller
@RequestMapping("momentLike")
public class MomentlikeAction {

	@Autowired
	MomentService momentService;
	@Autowired
	MomentlikeService momentlikeService;
	@Autowired
	UserService userService;
	
	//author by miao
	//为心情点赞,
	//参数userId点赞人Id,momentId心情Id
	@RequestMapping(value = "addMomentlike",method=RequestMethod.GET)
	@ResponseBody
	public Object momentlikes(@RequestParam("userId")String userId,@RequestParam("momentId")String momentId) {
		
		try {
			User user = userService.findById(Integer.parseInt(userId));
			Momentlike momentlike = new Momentlike();
			momentlike.setUser(user);
			Moment moment = momentService.findById(Integer.parseInt(momentId));
			momentlike.setMoment(moment);
			Date currentTime = new Date();
			Long time = currentTime.getTime();
			momentlike.setMomentlikepubtime(time);
			momentlikeService.save(momentlike);
			return "1";
		} catch (Exception e) {
			return "0";
		}

	}
	
	
	//author by miao
	//获取心情赞
	//momentId心情Id
	//成功返回有分页的momentLikeVo,失败返回""
	@RequestMapping(value="getLikes",method=RequestMethod.GET)
	@ResponseBody
	public Object getLikes(@RequestParam("momentId")String momentId,@RequestParam("currentPage")String currentPage,@RequestParam("amount")String amount){
		
		try {
			Page page = momentlikeService.findByMomentId(momentId,currentPage,amount);  
			List<Momentlike> momentlikes = page.getList();
			List<MomentLikeVo> momentLikeVos = new ArrayList<MomentLikeVo>();
			for (Momentlike momentlike : momentlikes) {
				MomentLikeVo momentLikeVo = new MomentLikeVo(momentlike);
				momentLikeVos.add(momentLikeVo);
			}
			page.setList(momentLikeVos);
			return page;
		} catch (Exception e) {
			return "";
		}

	}
	
	//取消点赞
	@RequestMapping("deleteMomentlike")
	@ResponseBody
	public Object deleteMomentlike(@RequestParam("momentlikeId")String momentlikeId){
		
		try {
			Momentlike momentlike = momentlikeService.findById(Integer.parseInt(momentlikeId));
			momentlikeService.delete(momentlike);
			return momentlikeId;
		} catch (Exception e) {
			System.out.println(e);
			return "0";
		}

		
		
	}
	
}
